package com.lijiaxing.pojo;

import com.lijiaxing.utils.enums.PermissionEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.tomcat.util.buf.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
@AllArgsConstructor
public class PermissionRes {
    private Integer total;
    private Permission[] permissions;

    @AllArgsConstructor
    @RequiredArgsConstructor
    @Data
    public static class Permission {
        @NonNull
        private Integer pid;
        @NonNull
        private String pname;
        private String permissions;
        private String permissionsName;

        public Permission(int pid, String pname, Integer[] permissions) {
            this.pid = pid;
            this.pname = pname;
            StringBuilder sb = new StringBuilder();
            for (Integer i : permissions) {
                sb.append(i);
            }
            String[] split = sb.toString().split("");
            List<String> list = Arrays.asList(split);
            this.permissions = StringUtils.join(list, ',');
            initPermissionName(permissions);
        }

        public void initPermissionName(Integer[] permissions) {
            ArrayList<String> list = new ArrayList<>();
            for (Integer s : permissions) {
                String value = PermissionEnum.getValue(s);
                list.add(value);
            }
            this.permissionsName = StringUtils.join(list, ',');

        }
    }

}